package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.Mode;
import ru.spb.osll.myfit.common.Content;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;
import ru.spb.osll.myfit.common.TreeNode;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Vassar Date: 09.03.2011 Time: 17:41:37 To
 * change this template use File | Settings | File Templates.
 */
public class ContentWidget extends PageElementWidget {
  private TreeNode m_treeNode;
  private PageElementType m_type;
  private String m_prefix;
  private Content m_content;
  private Tree m_tree;

  public ContentWidget(GWTWidgetFactory gwtWidgetFactory, Content content) {
    super(gwtWidgetFactory);
    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();
    m_content = content;
    m_treeNode = content.getTreeNode();
    m_type = content.getType();
    m_prefix = content.getPath();
    initWidget(createWidget());
    m_mode = Mode.EDIT;
  }

  protected Widget createWidget() {
    FlexTable layout = m_gwtWidgetFactory.createFlexTable();
    layout.setCellSpacing(6);

    Label capture = new Label("Contents:");
    capture.setStyleName(IStyleConstants.CONTENTS_HEADER_STYLE_NAME);

    m_tree = new Tree();
    List<TreeNode> children = m_treeNode.getChildren();
    Iterator<TreeNode> iterator = children.iterator();
    while (iterator.hasNext()) {
      TreeItem item = getTreeItem(iterator.next(), m_prefix);
      m_tree.addItem(item);
    }
    layout.setWidget(0, 0, capture);
    layout.setWidget(1, 0, m_tree);
    layout.setStyleName(IStyleConstants.CONTENTS_STYLE_NAME);
    return layout;
  }


  private TreeItem getTreeItem(TreeNode root, String prefix) {
    String pagePath;
    String name = root.getReference();

    if (!prefix.isEmpty())
      pagePath = prefix + "." + name;
    else
      pagePath = name;

    if (root.hasChild()) {
      name += "...";
    }

    String reference = "<a href=\"" + pagePath + "\">" + name + "</a>";

    TreeItem outerRoot = new TreeItem(new HTML(reference));

    if (root.getChildren() != null)
      addChildren(root.getChildren(), outerRoot, pagePath);

    return outerRoot;
  }

  private void addChildren(List<TreeNode> children, TreeItem root, String prefix) {
    Iterator<TreeNode> iterator = children.iterator();

    while (iterator.hasNext()) {
      TreeItem item = getTreeItem(iterator.next(), prefix);
      root.addItem(item);
    }
  }

  @Override
  public PageElement getPageElement() {
    return m_content;
  }


  @Override
  public void setOpen(boolean isOpen) {
    Iterator<TreeItem> it = m_tree.treeItemIterator();
    while (it.hasNext()) {
      it.next().setState(isOpen);
    }
  }

  @Override
  public PageElementType getType() {
    return m_type;
  }
}
